package cz.muni.fi.pa165.musiclibrary.impl.service.dto.translator;

import org.apache.commons.lang3.Validate;

import cz.muni.fi.pa165.musiclibrary.impl.database.entity.ISong;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.ISongDTO;
import cz.muni.fi.pa165.musiclibrary.impl.database.entity.Song;
import cz.muni.fi.pa165.musiclibrary.impl.service.dto.SongDTO;

/**
*
* @author Martin Majlath
* 2012-10-28T20:43:43+0100
*/
public final class SongDTOTranslator {
    
    /**
     * Translates {@link ISong} object into {@link ISongDTO} object.
     * @param song Song.
     * @return Instance of {@link ISongDTO} that correspons with given parameter.
     */
    public static ISongDTO translateToDTO(final ISong song) {
        if (song == null) {
            return null;
        }
        final ISongDTO songDTO = new SongDTO();      
        
        songDTO.setAlbum(AlbumDTOTranslator.translateToDTO(song.getAlbum()));
        songDTO.setArtist(ArtistDTOTranslator.translateToDTO(song.getArtist()));
        songDTO.setBitrate(song.getBitrate());
        songDTO.setComment(song.getComment());
        songDTO.setGenre(GenreDTOTranslator.translateToDTO(song.getGenre()));
        songDTO.setIdSong(song.getIdSong());
        songDTO.setLength(song.getLength());
        songDTO.setTitle(song.getTitle());
        songDTO.setTrackNumber(song.getTrackNumber());
                
        return songDTO;
    }
    
    /**
     * Translates {@link ISongDTO} object into {@link ISong} object.
     * @param song SongDTO.
     * @return Instance of {@link ISong} that correspons with given parameter.
     * @throws IllegalArgumentException If parameter is {@code null}.
     */
    public static ISong translateFromDTO(final ISongDTO songDTO) {
        Validate.isTrue(songDTO != null, "SongDTO should not be null.");
        final ISong song = new Song();
        
        song.setAlbum(AlbumDTOTranslator.translateFromDTO(songDTO.getAlbum()));
        song.setArtist(ArtistDTOTranslator.translateFromDTO(songDTO.getArtist()));
        song.setBitrate(songDTO.getBitrate());
        song.setComment(songDTO.getComment());
        song.setGenre(GenreDTOTranslator.translateFromDTO(songDTO.getGenre()));
        if(songDTO.getIdSong()!=null) {
            song.setIdSong(songDTO.getIdSong());
        }
        song.setLength(songDTO.getLength());
        song.setTitle(songDTO.getTitle());
        song.setTrackNumber(songDTO.getTrackNumber());

        return song;
    }

}